var cookieDomain = ".xinghaoshangcheng.com";
function updateCartCookie(cartItemsCookie){
	$.cookie('cartItems', cartItemsCookie, { expires: 7, path: '/',domain:cookieDomain}); 
}
function orderReadyDo(){
	//同步本地cookie
	//$.cookie('cartItems',$("#synchronizeCookieStr").val(), { expires: 7, path: '/' });
	updateCartCookie($("#synchronizeCookieStr").val());
	/*
	$("#address_div").click(function(){
		var token =$.cookie("token");
		var singleBuyFlag =$("#singleBuyFlag").val();
		var url = appPath+"/address/address.html?singleBuyFlag="+singleBuyFlag+"&chooseFlag=1&token="+token;
		self.location=url;
	});*/
	$("#submit_order").click(function(){
		//$(this).disable();
		var allNoNeedExpress = true;
		var noneedExpressCode = $("#noneedExpressCode").val();
		if(noneedExpressCode!=null&&noneedExpressCode!=''){
			$(".express-select option:selected").each(function(){
				if(noneedExpressCode.indexOf($(this).attr("code"))==-1){
					allNoNeedExpress = false;
				}
			});
		}
		if(!allNoNeedExpress){
			if($("#addressIsNull").val()=="1"){
				$.alert("请添加收货地址！");
				return false;
			}
			if($("input[name='addressId']:checked").length==0){
				$.alert("请选择收货地址！");
				return false;
			}
		}
		var pointBuyFlag = $("#pointBuyFlag").val();
		if("1"==pointBuyFlag){
			var userPoints = $("#userPoints").val();
			var consumePoints = $("#consumePoints").val();
			if(parseInt(userPoints)<parseInt(consumePoints)){//用户积分小于要消费的积分
				var msg ='您现有'+parseInt(userPoints)+'积分,所购商品需耗用'+parseInt(consumePoints)+"积分,积分不足,无法购买！";
				$.alert(msg);
				return false;
			}
			var pointConsumeMsg ='您现有'+parseInt(userPoints)+'积分,提交订单将扣除您个人'+parseInt(consumePoints)+"积分,确认提交？"
			myconfirm(pointConsumeMsg, doPointSubmit);
		}else{
			 dosubmit();
		}

	});
	$(".voucher-div").click(function() {
		$(".voucher-div").removeClass("item-selected");
		$(".voucher-top").addClass("voucher-top-bg");
		$(this).addClass("item-selected");
		$(this).find(".voucher-top").removeClass("voucher-top-bg");
		$("#voucherId").val($(this).attr("vid"));
		/* $(this).find(".item-selected-cancel").show(); */
	});
	$(".item-selected-cancel").click(function(e) {
		var parentDom = $(this).parent();
		parentDom.removeClass("item-selected");
		parentDom.find(".voucher-top").addClass("voucher-top-bg");
		$("#voucherId").val("");
		e.stopPropagation();
	});
	$(".remark-detail").click(function(e){
		$(".remark-detail").removeClass("expand");
		$(this).addClass("expand");
		e.stopPropagation(); 
	});
	var maxInput =120;
	$(".remark-input").keydown(function(){
		var inputLength = $(this).val().length;
		if(inputLength>maxInput){
			$(this).html($(this).html().substr(0,maxInput));
			inputLength = maxInput;
		}
		$(this).parent().find("span").html(inputLength+"/120");
	});
	$("body").click(function(){
		$(".remark-detail").removeClass("expand");
	})
}
function doPointSubmit(){
	dosubmit("1");
}

function dosubmit(pointBuyFlag){
	$.showLoading();
	var url = appPath+"/order/generateOrdersAjax.json"
	var temParam = $("#order_form").serialize();
	//console.log(temParam);
	$("#token-input").val($.cookie("token"));
	//$("#order_form").attr("action",url);
	//$("#order_form").submit();
	$.ajax({
	    type: 'post',
	    url: url,
	    data: $("#order_form").serialize(),
	    success: function(result) {
	    	if("1"==pointBuyFlag){
	    		//toPointPayResult(result.pointsConsumeResult);
	    		window.location.href = appPath+"/pay/result.htm?result="+result.pointsConsumeResult
	    	}else{
	    		if(result.orderInfo.state==21){//支付成功
	    			window.location.href = appPath+"/pay/result.htm?result=true"
	    		}else{
	    			topay(result);
	    		}
	    	}
	    },
	    error:function(msg){
	    	$.hideLoading();
			$.alert("系统异常!");
		}
	});
}

function topay(result){
	var orderCode = result.orderInfo.code;
	var synchronizeCookieStr = result.synchronizeCookieStr;
	//更新购物车cookie
	//$.cookie('cartItems', synchronizeCookieStr, { expires: 7, path: '/' });
	updateCartCookie(synchronizeCookieStr);
	// your code
	$.ajax({
		url:appPath+"/pay/pay.htm",
		dataType:'html',
		type:'post',
		data:{
			token: $.cookie("token"),
			orderCode:orderCode
		},
		success:function(newData){
			$.hideLoading();
			$('#mainlist').html(newData);
		},error:function(){
			$.hideLoading();
			$.alert("系统异常，请重试");
		}
	})
}
/**
 * 显示活动内容
 * @param _this
 * @returns {Boolean}
 */
function getActivityContent(activityId,activityType){
	//var activityId =$(_this).val();
	if("-1"==activityId){
		$("#order_gift_div").html("");
		return false;
	}
	if(!activityId.match(/^\d+$/)){
		return false;
	}
	$("#activityId").val(activityId);
	if(parseInt(activityType)!=10){//不是满赠，不用去查赠品信息
		$("#order_gift_div").html("");
		return false;
	}
	$.showLoading();
	$.ajax({
		url : appPath+"/activity/getGoodsInfos.htm",
		dataType : 'html',
		type : 'post',
		data : {
			activityId : activityId
		},
		success : function(data) {
			$.hideLoading();
			var giftInfoDiv = $(data).find("#order_gift_div");
			$('#order_gift_div').html($(giftInfoDiv[0]).html());
			/*$(".basic2 .carousel").jCarouselLite({
			    btnNext: ".basic2 .next",
			    btnPrev: ".basic2 .prev"
			});*/
			var obj = $('#jsCarousel');
			if(obj){
				$('#jsCarousel').jsCarousel({ onthumbnailclick: function(src) { 
					// 可在这里加入点击图片之后触发的效果
					$("#overlay_pic").attr('src', src);
					$(".overlay").show();
				}, autoscroll: false ,itemstodisplay:4,delay:5000
				});
				
				$(".overlay").click(function(){
					$(this).hide();
				});
			}
		},
		error : function() {
			$.hideLoading();
			$.alert("系统异常，请重试");
		}
	})
	return true;
}
function getActivityInfo(_this){
	var selVal = $(_this).val();
	var param = selVal.split(",");
	return getActivityContent(param[0],param[1]);
}